package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.BuzTaskUser;
import com.ruoyi.system.domain.form.BuzTaskAddData;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BuzTask;
import com.ruoyi.system.service.IBuzTaskService;
import com.ruoyi.web.core.config.JDCloudOSS;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动信息Controller
 * 
 * @author ruoyi
 * @date 2023-08-27
 */
@RestController
@RequestMapping("/system/buzTask")
public class BuzTaskController extends BaseController
{
    @Autowired
    private IBuzTaskService buzTaskService;
    
    @Value("${wxredirect_uri}")
    String previewUrl;

    /**
     * 查询活动信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:buzTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuzTask buzTask)
    {
        startPage();
        List<BuzTask> list = buzTaskService.selectBuzTaskList(buzTask);
        return getDataTable(list);
    }

    /**
     * 导出活动信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:buzTask:export')")
    @Log(title = "活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuzTask buzTask)
    {
        List<BuzTask> list = buzTaskService.selectBuzTaskList(buzTask);
        ExcelUtil<BuzTask> util = new ExcelUtil<BuzTask>(BuzTask.class);
        util.exportExcel(response, list, "活动信息数据");
    }

    /**
     * 获取活动信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:buzTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(buzTaskService.selectBuzTaskById(id));
    }

    /**
     * 新增活动信息
     */
   // @PreAuthorize("@ss.hasPermi('system:buzTask:add')")
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuzTask buzTask)
    {
        return toAjax(buzTaskService.insertBuzTask(buzTask));
    }

    /**
     * 修改活动信息
     */
   // @PreAuthorize("@ss.hasPermi('system:buzTask:edit')")
    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuzTask buzTask)
    {
        return toAjax(buzTaskService.updateBuzTask(buzTask));
    }

    /**
     * 删除活动信息
     */
    //@PreAuthorize("@ss.hasPermi('system:buzTask:remove')")
    @Log(title = "活动信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(buzTaskService.deleteBuzTaskByIds(ids));
    }

    /**
     * 根据活动状态获取活动列表
     *
     * @param status 活动状态
     * @return 结果
     */
    //@PreAuthorize("@ss.hasPermi('system:buzTask:getTaskList')")
    @GetMapping("/getTaskList")
    public AjaxResult getTaskList(String status)
    {
        List<BuzTask> list = buzTaskService.getBuzTaskListByStatus(status);
        return success(list);
    }
    /**
     * 根据活动id获取活动设置
     *
     */
   // @PreAuthorize("@ss.hasPermi('system:buzTask:getAllByTaskId')")
    @GetMapping("/getAllByTaskId")
    public AjaxResult getAllByTaskId(String taskId)
    {
        BuzTaskAddData result = buzTaskService.getAllByTaskId(taskId);
        return success(result);
    }
    /**
     * 保存或更新活动设置
     *
     */
   // @PreAuthorize("@ss.hasPermi('system:buzTask:saveTask')")
    @Log(title = "活动信息", businessType = BusinessType.INSERT)
    @PostMapping("/saveTask")
    public AjaxResult saveTask(@RequestBody BuzTaskAddData addData)
    {
        if(vaildCheck(addData)) {
            return success(buzTaskService.saveBuzTask(addData));
        }else{
            return error("设置内容不能为空");
        }
    }
    @Log(title = "活动相关图片上传", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadImg")
    public AjaxResult uploadImg(@RequestParam("imgfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);

             // 将生成的二维码图片上传到京东云OSS
            JDCloudOSS oss = new JDCloudOSS();
            avatar = oss.uploadFileJDCloud(avatar);
            
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
    private boolean vaildCheck(BuzTaskAddData addData){
        if(addData != null){
            if(addData.getBuzTask() != null){
                return true;
            }else {
                return false;
            }
        }else{
            return false;
        }
    }
    // 获取预览信息
    @PostMapping("/getPreviewUrl")
    public AjaxResult getPreviewUrl(String taskId) {
    	
    	return new AjaxResult(200, "成功",previewUrl+"/index/"+taskId );
    }
    @PostMapping("/publishTask/{taskId}")
    public AjaxResult publishTask(@PathVariable String taskId) {
    	
    	if(taskId!=null) {
    		boolean f = buzTaskService.publishTask(taskId);
    		if(f) {
    			return new AjaxResult(200,"发布成功");
    		}else {
    			return error("发布失败");
    		}
    	}else {
    		return error("活动id不能为空"); 
    	}
    	
    	
    	
    }
}
